// stores/counter.js
import { defineStore } from 'pinia'
interface Item {
  [key: string]: any
}
interface State {
  id?: number
  // addressDetail?: string,
  // areaCode?: string,
  // city?: string,
  // country?: string,
  // county?: string,
  isDefault?: boolean
  // name?: string,
  // province?: string,
  tel?: string
  name: string
  address: string
}
// Store 是使用 defineStore() 定义的，并且它需要一个唯一名称，作为第一个参数传递
export const useCounterStore = defineStore('counter', {
  /* =================================State================================ */
  state: () => {
    // state 推荐用箭头函数的方式 利于类型推断
    return {
      cart: JSON.parse(localStorage.getItem('cart') || '{}') || ([] as Item[]),
      // 全选状态
      allCheckeds: false,
      // 总价
      allPrices: 0,
      // 地址数据
      address: JSON.parse(localStorage.getItem('address')) || []
    }
  },
  /* =================================actions================================ */
  //
  actions: {
    //  这里使用常规函数 可以通过this进行访问store实例
    // 添加购物车
    addCart(item: any) {
      // console.log(item);
      const arr = this.cart.find((i: Item) => i.shop_spu_id === item.shop_spu_id) || ''

      if (arr.length == 0) {
        item.count = 1
        item.checked = false
        this.cart.push(item)
      } else {
        arr.count++
      }
      console.log(arr)
      console.log(this.cart)
      localStorage.setItem('cart', JSON.stringify(this.cart))
    },
    // 删除购物车
    deleteCart(indexs: number) {
      const data = JSON.parse(localStorage.getItem('cart') || '')
      data.forEach((item: Item, index: number) => {
        if (item.shop_spu_id === indexs) {
          data.splice(index, 1)
        }
      })
      localStorage.setItem('cart', JSON.stringify(data))
    },
    // 单选
    Dchecked(index: number) {
      const data = JSON.parse(localStorage.getItem('cart') || '')
      const item = data.find((i: Item) => i.shop_spu_id === index)
      console.log(item, '1111')
      if (item) {
        item.checked = !item.checked
      }

      localStorage.setItem('cart', JSON.stringify(data))
    },
    // 全选
    allChecked() {
      this.allCheckeds = !this.allCheckeds
      const data = JSON.parse(localStorage.getItem('cart') || '')

      data.forEach((item: Item) => {
        item.checked = this.allCheckeds
      })

      localStorage.setItem('allCheckeds', JSON.stringify(this.allCheckeds))
      localStorage.setItem('cart', JSON.stringify(data))
    },

    // 总价
    allPrice() {
      const data = JSON.parse(localStorage.getItem('cart') || '')
      let price = 0
      console.log(data, '333')
      data.forEach((item: Item) => {
        if (item.checked) {
          price += item.goods_info.price * item.count * 100
        }
      })
      this.allPrices = price
    },
    checkAllSelectedStatus() {
      const data = JSON.parse(localStorage.getItem('cart') || '')
      const allSelected = data.every((item: Item) => item.checked == true)
      console.log(allSelected, 'allSelected')
      this.allCheckeds = allSelected
    },
    // 数量加
    addCount(index: number) {
      const data = JSON.parse(localStorage.getItem('cart') || '')
      const item = data.find((i: Item) => i.shop_spu_id === index)
      if (item) {
        item.count++
      }
      localStorage.setItem('cart', JSON.stringify(data))
    },
    // 数量减
    subCount(index: number) {
      const data = JSON.parse(localStorage.getItem('cart') || '')
      const item = data.find((i: Item) => i.shop_spu_id === index)
      if (item) {
        item.count--
        if (item.count == 0) {
          return
        }
      }
      localStorage.setItem('cart', JSON.stringify(data))
    },

    // 地址添加
    addAddress(item: State) {
      this.address.forEach((v) => {
        if (v.isDefault) {
          v.isDefault = false
        }
      })
      this.address.unshift(item)
      localStorage.setItem('address', JSON.stringify(this.address))
    },
    // 删除
    deleteAddress(indexs: number) {
      console.log(indexs, 'indexs')

      const data = JSON.parse(localStorage.getItem('address') || '')
      data.forEach((item: State, index: number) => {
        if (item.id === indexs) {
          data.splice(index, 1)
        }
      })
      localStorage.setItem('address', JSON.stringify(data))
    },
    // 编辑
    updateAddress(items: State) {
      console.log(items, 'item')
      this.address.forEach((v) => {
        if (v.isDefault) {
          v.isDefault = false
        }
      })
      this.address.forEach((item: State) => {
        if (item.id === items.id) {
          item.name = items.name
          item.tel = items.tel
          item.address = items.address
          item.isDefault = items.isDefault
        }
      })
      localStorage.setItem('address', JSON.stringify(this.address))
      console.log(this.address, 'address')
    }
  }
})
